package cn.wenhaha.app.mapping.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckRole;
import cn.wenhaha.app.mapping.bean.dto.ConfigContext;
import cn.wenhaha.app.mapping.bean.dto.SyncObjectContext;
import cn.wenhaha.app.mapping.bean.vo.MappingListVO;
import cn.wenhaha.app.mapping.bean.vo.MappingVo;
import cn.wenhaha.app.mapping.service.MappingService;
import cn.wenhaha.common.bean.query.mapping.ListQuery;
import cn.wenhaha.common.core.domain.AjaxV1Result;
import com.jfinal.plugin.activerecord.Page;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;


/**
 * 映射管理控制器
 * --------
 *
 * @author ：wyndem
 * @Date ：Created in 2022-09-19 20:29
 */
@RestController
@RequestMapping("/api/v1/mapping")
public class MappingController {

    @Resource
    private MappingService mappingService;


    @PostMapping("save")
    @SaCheckRole("admin")
    public AjaxV1Result<String> save(@RequestBody @Validated SyncObjectContext context) {
        mappingService.save(context);
        return AjaxV1Result.newSuccess();
    }

    @PostMapping("saveConfig")
    @SaCheckRole("admin")
    public AjaxV1Result<String> saveConfig(@RequestParam String id,
                                           @RequestBody @Validated ConfigContext context) {
        mappingService.saveConfig(id,context);
        return AjaxV1Result.newSuccess();
    }

    @PostMapping("list")
    @SaCheckLogin
    public  AjaxV1Result<Page<MappingListVO>> list(@RequestBody @Validated ListQuery query){
        return  AjaxV1Result.newSuccess(mappingService.list(query));
    }


    @GetMapping("remove")
    @SaCheckLogin
    public AjaxV1Result<String> remove(Integer id){
        if (id!=null){
            mappingService.remove(id);
        }
        return AjaxV1Result.newSuccess();
    }



    @GetMapping("detail")
    @SaCheckLogin
    public  AjaxV1Result<MappingVo> detail(@RequestParam("id")String id){
        return  AjaxV1Result.newSuccess(mappingService.detail(id));
    }



    /**
     * <p>
     *    切换任务状态
     * </p>
     * @Author: Wyndem
     * @DateTime: 2022-12-09 21:31
     */
    @GetMapping("switch")
    @SaCheckLogin
    public  AjaxV1Result<String> switchMapping(@RequestParam("id")String id){
        mappingService.switchMapping(id);
        return  AjaxV1Result.newSuccess();
    }



    /**
     * <p>
     *    进行mapping测试
     * </p>
     * @Author: Wyndem
     * @DateTime: 2022-12-09 22:04
     */
    @GetMapping("test")
    @SaCheckLogin
    public AjaxV1Result<String> test(@RequestParam("id")String id){
        mappingService.test(id);
        return  AjaxV1Result.newSuccess();
    }

    @GetMapping("testOk")
    @SaCheckLogin
    public AjaxV1Result<String> testOk(@RequestParam("id")String id){
        mappingService.testOk(id);
        return  AjaxV1Result.newSuccess();
    }


}
